﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.ComponentModel;
using System.Xml;
using System.IO;
using SB.Logging;

namespace SB.Tools
{
    /// <summary>
    /// Diese Klasse beherbergt alle von uns öfter gebrauchten Methoden.
    /// </summary>
    public static class Commons
    {
        private static readonly ILogger log = LoggingFacade.GetLogger(typeof(Commons));

        /// <summary>
        /// Konvertiert einen String in einen MD5 Hashstring.
        /// </summary>
        /// <param name="str">Der zu verschlüsselnde String.</param>
        /// <returns>Den verschlüsselten String.</returns>
        public static string ConvertStringToHash(string str)
        {
            MD5 hasher = MD5.Create();
            byte[] data = hasher.ComputeHash(Encoding.Default.GetBytes(str));
            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                stringBuilder.Append(data[i].ToString("x2").ToLower());
            }

            return stringBuilder.ToString();
        }

        /// <summary>
        /// Kopiert alle Einträge aus einer IList in eine Bindinglist
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="input"></param>
        /// <returns></returns>
        public static void CopyIListToBindingList<T>(IList<T> source, ref BindingList<T> target)
        {
            foreach (T t in source)
            {
                if(!target.Contains(t))
                    target.Add(t);
            }
        }

        /// <summary>
        /// Kopiert alle Einträge aus einer IList in eine Bindinglist
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="input"></param>
        /// <returns></returns>
        public static void CopyIListToSortableBindingList<T>(IList<T> source, ref SortableBindingList<T> target)
        {
            foreach (T t in source)
            {
                if (!target.Contains(t))
                    target.Add(t);
            }
        }


        static string xmlFile = "..\\..\\..\\Shared Resources\\UserSettings\\userSettings.xml";

        /*public static void WriteDatagridColumnsToXML(BindingList<DisplayProperty> list, string name) {
            FileInfo file = new FileInfo(xmlFile);
            if (!file.Exists)
            {
                XmlTextWriter writer = new XmlTextWriter(xmlFile, null);
                writer.WriteStartDocument();
                writer.WriteStartElement("TicketLine");
                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Close();
            }

            XmlDocument doc = new XmlDocument();
            doc.Load(xmlFile);

            XmlNode datagrids = null;
            foreach (XmlNode x in doc.ChildNodes[1].ChildNodes)
            {
                if (x.Name == "Datagrids")
                {
                    datagrids = x;
                }
            }

            if (datagrids == null)
            {
                datagrids = doc.CreateNode(XmlNodeType.Element, "Datagrids", null);
                doc.ChildNodes[1].AppendChild(datagrids);
            }

            foreach (XmlNode y in datagrids.ChildNodes)
                if (y.Name == name)
                    datagrids.RemoveChild(y);

            XmlNode xml = doc.CreateNode(XmlNodeType.Element, name, null);
            foreach (DisplayProperty prop in list)
            {
                XmlNode child = doc.CreateNode(XmlNodeType.Element, prop.PropertyName, null);
                child.InnerText = prop.DisplayName;
                xml.AppendChild(child);
            }
            datagrids.AppendChild(xml);

            
            doc.Save(xmlFile);
        }

        public static void ReadDatagridColumnsFromXML(ref BindingList<DisplayProperty> list, string name)
        {
            
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(xmlFile);

                foreach (XmlNode x in doc.ChildNodes[1].ChildNodes)
                {
                    if (x.Name == "Datagrids")
                    {
                        foreach (XmlNode xml in x.ChildNodes)
                        {
                            if (xml.Name == name)
                            {
                                foreach (XmlNode y in xml.ChildNodes)
                                {
                                    list.Add(new DisplayProperty(y.Name, y.InnerText));
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception) { }
        }*/


    }
}
